"""鸿蒙仓颉API设计规范扫描执行器"""
from typing import Dict, Any, List
from app.scanners.base import ScanExecutor, ScanContext, ScanResult, ScanException
from app.scanners.registry import ScanExecutorRegistry


@ScanExecutorRegistry.register("harmony_api_scan")
class HarmonyAPIScanExecutor(ScanExecutor):
    """鸿蒙仓颉API设计规范扫描执行器"""
    
    def get_task_type(self) -> str:
        return "harmony_api_scan"
    
    def validate_params(self, scan_params: Dict[str, Any]) -> bool:
        """验证扫描参数"""
        # 基础参数验证，可以根据实际需求扩展
        return True
    
    def execute_scan(self, context: ScanContext) -> ScanResult:
        """
        执行扫描
        
        注意：这是一个示例实现，实际应该调用外部扫描工具
        """
        # TODO: 实际实现中应该调用外部扫描工具
        # 例如：subprocess调用命令行工具、调用Python库等
        
        # 模拟扫描结果（实际应从外部工具获取）
        issues = self._simulate_scan(context)
        
        # 构建扫描结果
        return ScanResult(
            issues=issues,
            summary={
                "total_issues": len(issues),
                "critical_count": sum(1 for i in issues if i.get("level") == "critical"),
                "warning_count": sum(1 for i in issues if i.get("level") == "warning"),
                "info_count": sum(1 for i in issues if i.get("level") == "info"),
            },
            metadata={
                "scan_range": context.scan_params.get("scan_range", "all"),
                "rule_version": context.scan_params.get("rule_version", "latest"),
            }
        )
    
    def _simulate_scan(self, context: ScanContext) -> List[Dict[str, Any]]:
        """模拟扫描（实际实现中应调用真实扫描工具）"""
        # 这是一个示例实现，返回模拟数据
        # 实际实现应该：
        # 1. 调用外部扫描工具（如命令行工具）
        # 2. 解析工具输出（JSON、CSV、XLSX等）
        # 3. 转换为标准格式
        
        return [
            {
                "rule_name": "API命名不规范",
                "level": "critical",
                "file_path": "/api/user/profile.ts",
                "line_number": 128,
                "description": "API名称未遵循驼峰命名规范，建议统一为 getUserProfile",
                "code_snippet": "export async function get_user_profile(id: string) {\n  return http.get(`/api/user/${id}/profile`);\n}",
                "api_name": "get_user_profile",
                "api_version": "1.0.0",
                "rule_type": "naming",
                "suggestion": "统一重命名为 getUserProfile，并更新调用方",
            },
            {
                "rule_name": "参数类型不一致",
                "level": "warning",
                "file_path": "/api/order/list.ts",
                "line_number": 42,
                "description": "返回字段 orderStatus 类型与文档描述不一致",
                "code_snippet": "interface OrderListResponse {\n  orderStatus: string;\n}",
                "api_name": "fetchOrderList",
                "api_version": "1.0.0",
                "rule_type": "type_consistency",
                "suggestion": "统一类型定义，修正文档说明",
            },
        ]
    
    def get_supported_extensions(self) -> List[str]:
        """返回支持的扩展字段"""
        return ["api_name", "api_version", "rule_type", "suggestion"]

